package com.rjwh.dingdong.client.activity.loginregist;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.activity.BaseActivity;
import com.rjwh.dingdong.client.util.ContactPhoneUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 用户注册
 *
 * @author 王小珂
 * @ClassName UserRegisterActivity
 * @Description TODO(这里用一句话描述这个类的作用)
 * @date 2016-3-10 下午3:58:46
 * @history 1.YYYY-MM-DD author: description:
 */
public class UserRegisterActivity extends BaseActivity {

    @BindView(R.id.user_register_phone_et)
    EditText userRegisterPhoneEt;
//    @BindView(R.id.user_register_cb)
//    CheckBox userRegisterCb;
    @BindView(R.id.user_register_text)
    TextView registerTx;
    @BindView(R.id.user_register_next_btn)
    Button nextBtn;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.app_bar_layout)
    AppBarLayout appBarLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register_layout);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        initToolbarHelper();

        userRegisterPhoneEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                final String content = s.toString();
                checkEditText(content);
            }
        });

//        registerTx.setOnClickListener(new OnClickListener() {
//
//            @Override
//            public void onClick(View v) {
//                // TODO 查看用户协议
//
//            }
//        });
    }

    private void checkEditText(final String phoneStr) {
        if (ContactPhoneUtils.isMobileNO(phoneStr)) {
            nextBtn.setBackgroundResource(R.drawable.long_btn);
            nextBtn.setClickable(true);
            nextBtn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(UserRegisterActivity.this, UserRegisterCodeActivity.class);
                    intent.putExtra("phone", phoneStr);
                    startActivity(intent);
                }
            });
        } else {
            nextBtn.setBackgroundResource(R.drawable.long_btn_unable);
            nextBtn.setClickable(false);
        }
    }

    private void initToolbarHelper() {
        this.setSupportActionBar(this.toolbar);
        assert getSupportActionBar() != null;
        getSupportActionBar().setTitle("用户注册");
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
}
